package com.zygh.policeasset.mapper;

import com.zygh.policeasset.pojo.Repair;
import com.zygh.policeasset.pojo.vo.RepairVO;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface RepairMapper {
    @Results(id = "map", value = {@Result(property = "assetsIds", column = "assets_ids"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "taskSourceId", column = "task_source_id"),
            @Result(property = "roadCrossId", column = "road_cross_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "flowNo", column = "flow_no"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "planCommenceTime", column = "plan_commence_time"),
            @Result(property = "requiredCompletionTime", column = "required_completion_time"),
            @Result(property = "budgetAmount", column = "budget_amount"),
            @Result(property = "maintenanceTime", column = "maintenance_time"),
            @Result(property = "appStatus", column = "app_status"),
            @Result(property = "approvalStepsId", column = "approval_steps_id"),
            @Result(property = "maintenanceRemark", column = "maintenance_remark")})
    @Select({"select  r.*,  af.*, ts.name as taskSourceName,rc.name as roadCrossName, ( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName,  ( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName  from repair r left join audit_flow af on r.flow_no = af.flow_no left join `user` u on r.user_id = u.id left join `task_source` ts on r.task_source_id = ts.id left join `road_cross` rc on r.road_cross_id = rc.id where r.id = #{id}"})
    RepairVO queryById(Integer num);

    @ResultMap({"map"})
    @Select({"<script>select  r.*,  af.*, ts.name as taskSourceName," +
            "rc.name as roadCrossName, ( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName, " +
            " ( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName " +
            " from repair r left join audit_flow af on r.flow_no = af.flow_no " +
            "left join `user` u on r.user_id = u.id " +
            "left join `task_source` ts on r.task_source_id = ts.id " +
            "left join `road_cross` rc on r.road_cross_id = rc.id " +
            "<where> " +
            "<if test='orderStatus != null '> and r.order_status=#{orderStatus} </if>" +
            "<if test='orderUserId != null '> and r.order_user_id=#{orderUserId} </if>" +
            "<if test=' status != null'>and af.app_status = #{status} </if>" +
            "<if test='userId != null '>and af.applicant=#{userId} </if>" +
            "  </where> order by r.id desc </script>"})
    List<RepairVO> queryAll(@Param("status") Integer num, @Param("userId") Integer num2, @Param("orderStatus") Integer num3, @Param("orderUserId") Integer num4);

    @ResultMap({"map"})
    @Select({"<script>select  r.*,  af.*, ts.name as taskSourceName,rc.name as roadCrossName, " +
            "( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName,  " +
            "( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName  " +
            "from repair r left join audit_flow af on r.flow_no = af.flow_no " +
            "left join `user` u on r.user_id = u.id " +
            "left join `task_source` ts on r.task_source_id = ts.id " +
            "left join `road_cross` rc on r.road_cross_id = rc.id <where> " +
            "<if test='orderStatus != null '> and r.order_status=#{orderStatus} </if>" +
            "<if test='userId != null '> and r.order_user_id=#{userId} </if> and af.app_status = 2 order by r.id desc</where>" +
            " </script>"})
    List<RepairVO> appQueryOrder(@Param("status") Integer num, @Param("userId") Integer num2, @Param("orderStatus") Integer num3, @Param("orderUserId") Integer num4);

    @ResultMap({"map"})
    @Select({"<script>select  r.*,  af.*, ts.name as taskSourceName,rc.name as roadCrossName, " +
            "( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName, " +
            " ( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName  " +
            "from repair r left join audit_flow af on r.flow_no = af.flow_no " +
            "left join `user` u on r.user_id = u.id " +
            "left join `task_source` ts on r.task_source_id = ts.id " +
            "left join `road_cross` rc on r.road_cross_id = rc.id " +
            "<where> " +
            "<if test='orderStatus != null '> and r.order_status=#{orderStatus} </if>" +
            "<if test='orderUserId != null '> and r.order_user_id=#{orderUserId} </if> " +
            "and af.app_status = 2 order by r.id desc" +
            "</where> </script>"})
    List<RepairVO> selectByOrderUserId(@Param("orderStatus") Integer orderStatus, @Param("orderUserId") Integer orderUserId);

    @ResultMap({"map"})
    @Select({"<script>select  r.*,  af.*, ts.name as taskSourceName,rc.name as roadCrossName, " +
            "( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName, " +
            " ( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName  " +
            "from repair r left join audit_flow af on r.flow_no = af.flow_no " +
            "left join `user` u on r.user_id = u.id " +
            "left join `task_source` ts on r.task_source_id = ts.id " +
            "left join `road_cross` rc on r.road_cross_id = rc.id " +
            "<where> " +
            " r.id in <foreach collection='ids' item='item' index='index' open='(' separator=',' close=')'> #{item} </foreach>" +
            "</where> </script>"})
    List<RepairVO> queryByIds(@Param("ids") String[] ids);

    @Insert({"insert into repair(assets_ids,type,task_source_id,urgent,content,road_cross_id,user_id,flow_no,create_time,plan_commence_time,required_completion_time,budget_amount,order_user_id) values(#{assetsIds},#{type},#{taskSourceId},#{urgent},#{content},#{roadCrossId},#{userId},#{flowNo},#{createTime},#{planCommenceTime},#{requiredCompletionTime},#{budgetAmount},#{orderUserId})"})
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(Repair repair);

    @Update({"<script>update repair <set><if test='assetsIds != null and assetsIds.length &gt; 0'> assets_ids=#{assetsIds},</if><if test='type != null '> type=#{type},</if><if test='taskSourceId != null '> task_source_id=#{taskSourceId},</if><if test='urgent != null'> urgent=#{urgent},</if><if test='content != null and content.length &gt; 0'> content=#{content},</if><if test='roadCrossId != null '> road_cross_id=#{roadCrossId},</if><if test='userId != null '> user_id=#{userId},</if><if test='flowNo != null and flowNo.length &gt; 0'> flow_no=#{flowNo},</if><if test='updateTime != null and updateTime.length &gt; 0'> update_time=#{updateTime},</if></set> where id = #{id}</script>"})
    int update(Repair repair);

    @Delete({"delete from repair where id = #{id}"})
    int deleteById(Integer num);

    @Update({"<script>update repair <set><if test='userId != null'> order_user_id=#{userId},</if><if test='orderStatus != null'>order_status = #{orderStatus}, </if><if test='maintenanceTime != null and maintenanceTime.length &gt; 0'>maintenance_time = #{maintenanceTime}, </if><if test='maintenanceRemark != null and maintenanceRemark.length &gt; 0'>maintenance_remark = #{maintenanceRemark}, </if></set> where id = #{id}</script>"})
    int issuedOrder(@Param("id") Integer num, @Param("userId") Integer num2, @Param("orderStatus") Integer num3, @Param("maintenanceTime") String str, @Param("maintenanceRemark") String str2);

    @Update({"<script>update repair <set><if test='userId != null'> order_user_id=#{userId},</if></set> where flow_no = #{flowNo}</script>"})
    int issued(@Param("userId") Integer num, @Param("flowNo") String str);

    @ResultMap({"map"})
    @Select({"<script>" +
            "select r.*,ts.name as taskSourceName,rc.name as roadCrossName," +
            "( SELECT NAME FROM `user` u WHERE r.user_id = u.id ) as userName," +
            "( SELECT NAME FROM `user` u WHERE r.order_user_id = u.id )  as orderUserName from " +
            "repair r left join `user` u on r.user_id = u.id " +
            "left join `task_source` ts on r.task_source_id = ts.id " +
            "left join `road_cross` rc on r.road_cross_id = rc.id " +
            "<where>  r.id in <foreach collection='ids' item='item' index='index' open='(' separator=',' close=')'> " +
            "#{item} </foreach></where>  </script>"})
    List<RepairVO> findByIds(@Param("ids") String[] numArr);
}